package uvt.gui.mainframe;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import uvt.gui.mainframe.object.command.Command;
import uvt.gui.mainframe.object.menuitem.AddMapElementsMenuItem;
import uvt.gui.mainframe.object.menuitem.SetPositionsMenuItem;
import uvt.gui.mainframe.object.menuitem.ShowGraphMenuItem;
import uvt.gui.mainframe.searchroute.SearchRoutePanel;
import uvt.gui.mainframe.subpanel.PanelAddParent;
import uvt.map.PositionsSettings;

public class MainFrame extends JFrame implements ActionListener {
	
	private JPanel parentPanel;
	private SearchRoutePanel searchRoutePanel;
	private CreateTransportationNetworkPanel transportationNetworkPanel;
	private MapPanel mapPanel;
	
	private JMenuBar menuBar;
	private JMenu menuItem;		
	private static AddMapElementsMenuItem chkAddMapElements;
	private static SetPositionsMenuItem chkSetPositions;
	private ShowGraphMenuItem showGraph;

	public MainFrame(String title) {
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(screen.getWidth() / 2 - 575),
				    (int)(screen.getHeight() / 2 - 215));
		setTitle(title);
		setMinimumSize(new Dimension(1150,430));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		parentPanel = new JPanel(new BorderLayout());
		setContentPane(parentPanel);
		
		mapPanel = new MapPanel();	
		parentPanel.add(mapPanel,BorderLayout.CENTER);
		
		PositionsSettings.getInstance().setMap(mapPanel);	
		
		searchRoutePanel = new SearchRoutePanel(mapPanel);
		parentPanel.add(searchRoutePanel,BorderLayout.NORTH);
		
		PanelAddParent.setMapViewer(mapPanel);	
		transportationNetworkPanel = new CreateTransportationNetworkPanel();
		transportationNetworkPanel.setBackground(Color.white);
		
		menuBar = new JMenuBar();
		menuItem = new JMenu("Edit");	
		
		chkAddMapElements = new AddMapElementsMenuItem(parentPanel,searchRoutePanel,
				                                       transportationNetworkPanel);
		chkAddMapElements.addActionListener(this);
		menuItem.add(chkAddMapElements);
		
		chkSetPositions = new SetPositionsMenuItem();
		chkSetPositions.addActionListener(this);
		menuItem.add(chkSetPositions);
		
		showGraph = new ShowGraphMenuItem();
		showGraph.addActionListener(this);
		menuItem.add(showGraph);
		
		menuBar.add(menuItem);
		setJMenuBar(menuBar);
	}
	
	public void actionPerformed(ActionEvent ac) {
		((Command)ac.getSource()).execute();		
	}
	
	public static JCheckBoxMenuItem getChkAddMapElements() {
		return chkAddMapElements;
	}
	
	public static JCheckBoxMenuItem getChkSetPositions() {
		return chkSetPositions;
	}
	
}
